﻿using ProductSystem.BLL.Common;
using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL
{
    public partial class Food_Bll
    {
        public static List<Food> GetAllList()
        {
            List<Food> list = new List<Food>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.Food.Where(x => x.Status == 1).ToList();
            }
            return list;
        }

        public static List<Food> GetList(ref PageInfo pageInfo)
        {
            List<Food> list = new List<Food>();
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                list = dbContext.Food.Where(x => x.Status == 1).OrderByDescending(m => m.ID).ToList();
                pageInfo.TotalCount = list.Count;
                list = list.Skip((pageInfo.CurPage - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList();
            }
            return list;
        }

        public static Food GetData(int id)
        {
            List<Food> list = GetAllList();
            return list.FirstOrDefault(t => t.ID == id);
        }

        public static ReturnStatus DeleteFood(string ids)
        {
            ReturnStatus rs;
            try
            {
                using (var dbContext = new EntitiesModel())
                {
                    string[] idList = ids.Split(',');
                    for (int i = 0; i < idList.Length; i++)
                    {
                        int id = StringExtension.GetInt(idList[i], 0);
                        Food model = dbContext.Food.FirstOrDefault(o => o.ID == id);
                        dbContext.Delete(model);
                    }
                    dbContext.SaveChanges();
                }
                rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptDeleteSuccess);
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptDeleteFail);
            }
            return rs;
        }
    }
}
